package core.service;

import java.util.Map;

import util.Sessions;

import com.opensymphony.xwork2.ActionContext;

public class BaseService {
	//分页
	@SuppressWarnings("rawtypes")
	public int getCurPagea(){
		ActionContext context = ActionContext.getContext(); 
		Map params = context.getParameters();
		if(params.containsKey("page"))
			return Integer.parseInt(((String[])params.get("page"))[0]);
		else
			return 1;
	}

	public String pageHtmla(int totalNum,int perNum){
		if(totalNum<1) return "";
		else{
			Sessions.set("pagetotal",(int)Math.ceil((double)totalNum/(double)perNum));
			return "";
		}
		//return this.pageList(getCurPage(),(int)Math.ceil((double)totalNum/(double)perNum));
	}
	
	@SuppressWarnings("unused")
	private String pageList(int currentPage,int totalPage){
		//ActionContext context = ActionContext.getContext();
		//HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
		//String this_url=request.getContextPath();
		
		if(currentPage<1 || currentPage> totalPage) currentPage=1;
		
		String first="<a href='?page=1'>1...</a> ";
		String last=" <a href='?page="+totalPage+"'>..."+totalPage+"</a> ";
		
		String total=" 第"+currentPage+"页/共"+totalPage+"页";
		
		String pre=currentPage!=1?"<a href='?page="+(currentPage-1)+"'>上一页</a>":"";
		String later=currentPage!=totalPage?"<a href='?page="+(currentPage+1)+"'>下一页</a>":"";
		
		String list="";
		int i;
		if(totalPage>10){
			if(currentPage<=6){
				for(i=1;i<=10;i++)
					list+=" <a href='?page="+i+"'>"+i+"</a> ";
				list+=last;
			}else if(currentPage>=totalPage-5){
				list+=first;
				for(i=totalPage-9;i<=totalPage;i++)
					list+=" <a href='?page="+i+"'>"+i+"</a> ";
			}else{
				list+=first;
				for(i=currentPage-4;i<=currentPage+4;i++)
					list+=" <a href='?page="+i+"'>"+i+"</a> ";
				list+=last;
			}
		}else{
			for(i=1;i<=totalPage;i++)
				list+=" <a href='?page="+i+"'>"+i+"</a> ";
		}		
		return pre+list+later+total;
	}
	
	public void p(Object p){
		System.out.println(p);
	}
}
